<?php
class Controller_Advertiserdata extends Controller_Abstract
{
	function actionadd()
	{
		if ($this->_context->isPOST()) {
			$advertiser = $this->_context->post('advertiser', '');
			$site = $this->_context->post('site', '');
			$placement = $this->_context->post('placement', '');
			$click = $this->_context->post('click', 0);
			$revenue = $this->_context->post('revenue', 0);
			$date = $this->_context->post('date');
			$dataModel = new Data();
			if ($dataModel->addAdvertiserData($advertiser, $site, $placement, $click, $revenue, $date)) {
				$infos = array();
				$infos[] = Util::getMessage(12004);
				$_SESSION['infos'] = $infos;
				return $this->_redirect(url('advertiserdata/index'));
			}
		}
		if (isset($_SESSION['infos'])) {
			$this->_view['infos'] = $_SESSION['infos'];
			unset($_SESSION['infos']);
		}
		$this->_view['sites'] = DataUtil::getOptionsForSites();
		$this->_view['advertisers'] = DataUtil::getOptionsForAdvertisers();
	}
	
	function actionIndex()
	{
		if (isset($_SESSION['infos'])) {
			$this->_view['infos'] = $_SESSION['infos'];
			unset($_SESSION['infos']);
		}
		$advertiserId = $this->_context->get('advertiser_id', '');
		$siteId = $this->_context->get('site_id', 0);
		$dateStart = $this->_context->get('date_start', '');
		$dateEnd = $this->_context->get('date_end', '');
		$dataModel = new Data();
		$this->_view['advertiserData'] = $dataModel->search($advertiserId, $siteId, $dateStart, $dateEnd);
		$this->_view['sites'] = DataUtil::getOptionsForSites();
		$this->_view['advertisers'] = DataUtil::getOptionsForAdvertisers();
		$this->_view['site_id'] = $siteId;
		$this->_view['advertiser_id'] = $advertiserId;
		$this->_view['date_start'] = $dateStart;
		$this->_view['date_end'] = $dateEnd;
	}
	
	function actionEdit()
	{
		if (isset($this->_context->id)) {
			$id = $this->_context->id;
			if ($this->_context->isPOST()) {
				$advertiser = $this->_context->post('advertiser', '');
				$site = $this->_context->post('site', '');
				$placement = $this->_context->post('placement', '');
				$click = $this->_context->post('click', 0);
				$revenue = $this->_context->post('revenue', 0);
				$date = $this->_context->post('date');
				$dataModel = new Data();
				if ($dataModel->editAdvertiserData($id, $advertiser, $site, $placement, $click, $revenue, $date)) {
					$infos = array();
					$infos[] = Util::getMessage(12005);
					$_SESSION['infos'] = $infos;
					return $this->_redirect(url('advertiserdata/index'));
				}
			} else {
				$dataModel = new Data();
				$this->_view['data'] = $dataModel->getAdvertiserDataById($id);
				$this->_view['sites'] = DataUtil::getOptionsForSites();
				$this->_view['advertisers'] = DataUtil::getOptionsForAdvertisers();
			}
		}		
	}
	
	function actionDelete()
	{
		if (isset($this->_context->id)) {
			$id = $this->_context->id;
			$dataModel = new Data();
			if ($dataModel->deleteAdvertiserDataById($id)) {
				$infos = array();
				$infos[] = Util::getMessage(12006);
				$_SESSION['infos'] = $infos;
				return $this->_redirect(url('advertiserdata/index'));
			}
		}
	}
}